﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Client.Logic.Utility;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities.Enumerator;
using VMind.Global;

namespace PPI
{
    public partial class frmAssociateUtility : frmBase,IAssociateUtility
    {
        private DatabaseTypes m_DatabaseType;
        private int m_IdToDuplicate;
        private string m_ConnectionString;
        private AssociateUtilityPresenter m_Presenter;
        private double m_latitude;
        private double m_longitude;

        public frmAssociateUtility()
        {
            InitializeComponent();
            base.MenuMain.Visible = false;
            this.FormSize = FormSizes.Medium;
        }

        private void HideShowUtility(bool hide)
        {
            ddlNearAssets.Visible = !hide;
            btnSave.Visible = !hide;
            lblUtility.Visible = !hide;
            if (hide)
                ddlNearAssets.SelectedIndex = 0;
        }
        
        private void btnAssociateUtility_Click(object sender, EventArgs e)
        {
            HideShowUtility(btnSave.Visible);
        }

        private void btnCreatenew_Click(object sender, EventArgs e)
        {
            m_Presenter.CreateNew();
        }

        private void frmAssociateUtility_Load(object sender, EventArgs e)
        {
            try
            {
                m_Presenter = new AssociateUtilityPresenter(this);
                ddlNearAssets.DataSource =LUtility_Asset_XMissionStructure.SelectNearLatitudeLongitude(m_latitude, m_longitude);
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            m_Presenter.Associate();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region IAssociateUtility

        public DatabaseTypes DatabaseType
        {
            get { return m_DatabaseType; }
            set { m_DatabaseType = value; }
        }

        public int IdToDuplicate
        {
            get { return m_IdToDuplicate; }
            set { m_IdToDuplicate = value; }
        }

        public int IdUtility
        {
            get { return ConvertHelper.ToInt32(ddlNearAssets.SelectedValue); }
        }

        public string ConnectionString
        {
            get { return m_ConnectionString; }
            set { m_ConnectionString = value;    }
        }

        public void ShowError()
        {
            MessageBox.Show("Error");
        }

        public void ShowSuccesfull()
        {
            this.Close();
        }

        public void ShowValidateMessage()
        {
            MessageBox.Show("Please UtilityId, must be greater than 0");
        }

        public double Latitude
        {
            get { return m_latitude; }
            set { m_latitude = value; }
        }

        public double Longitude
        {
            get { return m_longitude; }
            set { m_longitude = value; }
        }

        #endregion

        private void frmAssociateUtility_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            if (m_Presenter.IsSaved)
                this.DialogResult = DialogResult.OK;

        }

    }
}
